package com.ruoyi.system.controller;

import com.alibaba.excel.EasyExcel;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.InvoiceGroup;
import com.ruoyi.system.domain.ProvideAllRecords;
import com.ruoyi.system.domain.ProvideInfo;
import com.ruoyi.system.excel.*;
import com.ruoyi.system.service.impl.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/excel")
public class ExcelController {

    @Autowired
    private InvoiceRecordsServiceImpl invoiceRecordsService;
    @Autowired
    private InvoiceGroupServiceImpl invoiceGroupService;
    @Autowired
    private ProvideRecordsServiceImpl provideRecordsService;
    @Autowired
    private ProvideInfoServiceImpl provideInfoService;
    @Autowired
    private SysDeptServiceImpl sysDeptService;
    @Autowired
    private ProvideAllRecordsServiceImpl provideAllRecordsService;
    @Autowired
    private ProvideWareServiceImpl provideWareService;
    @Autowired
    private SysUserServiceImpl sysUserService;
    @Autowired
    private InvoiceRecordsAuditServiceImpl invoiceRecordsAuditService;
    @Autowired
    private SysAuditServiceImpl sysAuditService;


    /**
     * 导入
     * <p>
     * 1. 创建excel对应的实体对象
     * <p>
     * 2. 由于默认一行行的读取excel，所以需要创建excel一行一行的回调监听器，参照{@link}
     * <p>
     * 3. 直接读即可
     */

    @RequestMapping("/upload")
    @Transactional(rollbackFor = IOException.class)
    public AjaxResult upload(MultipartFile file) throws IOException {
        Map<String,Object> resultMap = new HashMap<>();
        try {
            EasyExcel.read(file.getInputStream(), new NoModelDataListener(invoiceRecordsService)).sheet().doRead();
            resultMap.put("code", 200);
            resultMap.put("message", "导入数据成功");
        }catch (Exception e){
            resultMap.put("code",203);
            resultMap.put("message",e.getMessage());
        }
        return  AjaxResult.success(resultMap);
    }

    /**
     * 导入
     * <p>
     * 1. 创建excel对应的实体对象
     * <p>
     * 2. 由于默认一行行的读取excel，所以需要创建excel一行一行的回调监听器，参照{@link}
     * <p>
     * 3. 直接读即可
     */
    @RequestMapping("/upload1")
    @Transactional(rollbackFor = IOException.class)
    public AjaxResult upload1(MultipartFile file) throws IOException {
        Map<String,Object> resultMap = new HashMap<>();
        try {
            EasyExcel.read(file.getInputStream(), new NoModelDataListener1(invoiceGroupService)).sheet("轮胎").doRead();
            resultMap.put("code", 200);
            resultMap.put("message", "导入数据成功");
        }catch (Exception e){
            resultMap.put("code",203);
            resultMap.put("message",e.getMessage());
        }
        return  AjaxResult.success(resultMap);
    }

    /**
     * 导入
     * <p>
     * 1. 创建excel对应的实体对象
     * <p>
     * 2. 由于默认一行行的读取excel，所以需要创建excel一行一行的回调监听器，参照{@link}
     * <p>
     * 3. 直接读即可
     */
    @RequestMapping("/upload2")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult upload2(MultipartFile file,@RequestParam("time")String time)  throws IOException{
        Map<String,Object> resultMap = new HashMap<>();
        try {
            invoiceRecordsService.deleteUpload(time);
            invoiceRecordsAuditService.deleteUpload(time);
            EasyExcel.read(file.getInputStream(), new NoModelDataListener2(invoiceRecordsService,sysDeptService,invoiceRecordsAuditService,sysAuditService,time)).sheet().doRead();
            invoiceRecordsService.updateStatus(time);
            //添加产品类型表
            List<String> list = invoiceGroupService.selectProductCode();
            List<String> list1 = invoiceRecordsService.selectProductCode(list);
            for (String str:list1) {
                InvoiceGroup invoiceGroup = new InvoiceGroup();
                invoiceGroup.setProductCode(str);
                invoiceGroup.setProductGroup("配件");
                invoiceGroupService.insertInvoiceGroup(invoiceGroup);
            }
            resultMap.put("code", 200);
            resultMap.put("message", "导入数据成功");
        }catch (Exception e){
            throw new IOException(e.getMessage());
        }
        return  AjaxResult.success(resultMap);
    }

    /**
     * 导入
     * <p>
     * 1. 创建excel对应的实体对象
     * <p>
     * 2. 由于默认一行行的读取excel，所以需要创建excel一行一行的回调监听器，参照{@link}
     * <p>
     * 3. 直接读即可
     */
    @RequestMapping("/upload3")
    @Transactional(rollbackFor = IOException.class)
    public AjaxResult upload3(MultipartFile file) throws IOException {
        Map<String,Object> resultMap = new HashMap<>();
        try {
            EasyExcel.read(file.getInputStream(), new NoModelDataListener3(invoiceRecordsService,sysDeptService)).sheet().doRead();
            resultMap.put("code", 200);
            resultMap.put("message", "导入数据成功");
        }catch (Exception e){
            throw new IOException(e.getMessage());
        }
        return  AjaxResult.success(resultMap);
    }


    /**
     * 导入
     * <p>
     * 1. 创建excel对应的实体对象
     * <p>
     * 2. 由于默认一行行的读取excel，所以需要创建excel一行一行的回调监听器，参照{@link}
     * <p>
     * 3. 直接读即可
     */
    @RequestMapping("/upload4")
    @Transactional(rollbackFor = IOException.class)
    public AjaxResult upload4(MultipartFile file) throws IOException {
        Map<String,Object> resultMap = new HashMap<>();
        try {
            EasyExcel.read(file.getInputStream(), new NoModelDataListener4(invoiceRecordsService,sysDeptService)).sheet().doRead();
            resultMap.put("code", 200);
            resultMap.put("message", "导入数据成功");
        }catch (Exception e){
            throw new IOException(e.getMessage());
        }
        return  AjaxResult.success(resultMap);
    }


    /**
     * 导入
     * <p>
     * 1. 创建excel对应的实体对象
     * <p>
     * 2. 由于默认一行行的读取excel，所以需要创建excel一行一行的回调监听器，参照{@link}
     * <p>
     * 3. 直接读即可
     */
    @RequestMapping("/upload5")
    @Transactional(rollbackFor = IOException.class)
    public AjaxResult upload5(MultipartFile file, @RequestParam("time")String time) throws IOException {
        Map<String,Object> resultMap = new HashMap<>();
        try {
            provideRecordsService.deleteUpload(time);
            provideAllRecordsService.deleteUpload(time);
            EasyExcel.read(file.getInputStream(), new NoModelDataListener5(provideRecordsService,sysDeptService,time,provideWareService)).sheet().doRead();
            //添加配件类型
            List<String> list = invoiceGroupService.selectProductCode();
            List<String> list1 = provideRecordsService.selectProductCode(list);
            for (String str:list1) {
                InvoiceGroup invoiceGroup = new InvoiceGroup();
                invoiceGroup.setProductCode(str);
                invoiceGroup.setProductGroup("配件");
                invoiceGroupService.insertInvoiceGroup(invoiceGroup);
            }

            //添加配件总表计算
            List<Map<String, Object>> maps = provideAllRecordsService.selectAllRecords(time);
            if(maps!=null&& maps.size()>0){
                DecimalFormat df = new DecimalFormat("0.00##");
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String updateTime = sdf.format(new Date());
                for (Map<String,Object> map:maps) {
                    String create_time = map.get("create_time").toString();
                    String provide = map.get("provide").toString();
                    String money = map.get("money").toString();
                    String priceMoney = map.get("priceMoney").toString();
                    money = df.format(Double.valueOf(money));
                    ProvideAllRecords provideAllRecords = new ProvideAllRecords();
                    provideAllRecords.setProvide(provide);
                    provideAllRecords.setCreateTime(create_time);
                    provideAllRecords.setFinanceCode("");
                    provideAllRecords.setTaxRed("0");
                    provideAllRecords.setActMoney("0");
                    provideAllRecords.setPlatformMoney(priceMoney);
                    provideAllRecords.setPayableMoney(money);
                    provideAllRecords.setUnpaidMoney(money);
                    provideAllRecords.setValuationTax("0");
                    provideAllRecords.setUpdateTime(updateTime);
                    provideAllRecords.setStatus("0");
                    provideAllRecordsService.insertProvideAllRecords(provideAllRecords);
                }
            }
            //修改配件总表财务编码
            ProvideAllRecords provideAll = new ProvideAllRecords();
            provideAll.setCreateTime(time);
            List<ProvideAllRecords> provideAllRecords = provideAllRecordsService.selectProvideAllRecordsList(provideAll);
            List<ProvideInfo> provideInfos = provideInfoService.selectProvideInfoList(new ProvideInfo());
            for (ProvideAllRecords provideAllRecords1:provideAllRecords) {
                for (ProvideInfo provideInfo:provideInfos) {
                    if(provideAllRecords1.getProvide().equals(provideInfo.getName())){
                        provideAllRecords1.setFinanceCode(provideInfo.getFinanceCode());
                        provideAllRecordsService.updateProvideAllRecords(provideAllRecords1);
                    }
                }
            }
            resultMap.put("code", 200);
            resultMap.put("message", "导入数据成功");
        }catch (Exception e){
            throw new IOException(e.getMessage());
        }
        return  AjaxResult.success(resultMap);
    }


    /**
     * 导入
     * <p>
     * 1. 创建excel对应的实体对象
     * <p>
     * 2. 由于默认一行行的读取excel，所以需要创建excel一行一行的回调监听器，参照{@link}
     * <p>
     * 3. 直接读即可
     */
    @RequestMapping("/upload6")
    @Transactional(rollbackFor = IOException.class)
    public AjaxResult upload6(MultipartFile file) throws IOException {
        Map<String,Object> resultMap = new HashMap<>();
        try {
            EasyExcel.read(file.getInputStream(), new NoModelDataListener6(provideInfoService)).sheet().doRead();
            resultMap.put("code", 200);
            resultMap.put("message", "导入数据成功");
        }catch (Exception e){
            resultMap.put("code",203);
            resultMap.put("message",e.getMessage());
        }
        return  AjaxResult.success(resultMap);
    }

    /**
     * 导入
     * <p>
     * 1. 创建excel对应的实体对象
     * <p>
     * 2. 由于默认一行行的读取excel，所以需要创建excel一行一行的回调监听器，参照{@link}
     * <p>
     * 3. 直接读即可
     */
    @RequestMapping("/uploadUser")
    @Transactional(rollbackFor = IOException.class)
    public AjaxResult uploadUser(MultipartFile file) throws IOException {
        Map<String,Object> resultMap = new HashMap<>();
        try {
            EasyExcel.read(file.getInputStream(), new NoModelDataListener7(sysUserService,sysDeptService)).sheet().doRead();
            resultMap.put("code", 200);
            resultMap.put("message", "导入数据成功");
        }catch (Exception e){
            resultMap.put("code",203);
            resultMap.put("message",e.getMessage());
        }
        return  AjaxResult.success(resultMap);
    }

    /**
     * 导入
     * <p>
     * 1. 创建excel对应的实体对象
     * <p>
     * 2. 由于默认一行行的读取excel，所以需要创建excel一行一行的回调监听器，参照{@link}
     * <p>
     * 3. 直接读即可
     */
    @RequestMapping("/uploadAudit")
    @Transactional(rollbackFor = IOException.class)
    public AjaxResult uploadAudit(MultipartFile file) throws IOException {
        Map<String,Object> resultMap = new HashMap<>();
        try {
            EasyExcel.read(file.getInputStream(), new NoModelDataListener8(sysAuditService)).sheet().doRead();
            resultMap.put("code", 200);
            resultMap.put("message", "导入数据成功");
        }catch (Exception e){
            resultMap.put("code",203);
            resultMap.put("message",e.getMessage());
        }
        return  AjaxResult.success(resultMap);
    }
}
